#!/bin/bash
source /etc/profile
export LANG=en_US.UTF-8
Where="/usr/bin/whereis"

AbsPath=$(dirname $0)
echo "\e[31m $AbsPath \e[0m"
if [[ "$AbsPath" =~ ^[.] || "$AbsPath" =~ [^/] ]];then
    cd $AbsPath
    AbsPath="`pwd`"
fi

Process="`echo $AbsPath |awk -F/ '{print $(NF-1)}'`"


ErrorderMesg1="\e[31m没有找到必要的系统命令: $Where 或 which \e[0m"
ErrorderMesg2="\e[31m没有找到JPS命令\e[0m"

ErrconfMesg1="\e[31m配置文件为空\e[0m"
ErrconfMesg2="\e[31m配置文件不存在\e[0m"

ErrproMesg1="\e[31m$Process 已存在\e[0m"

Errcode=1
OKcode=0

# judge whereis and which
if ! [ "`$Where which 2>/dev/null`" ];then
    echo -e $ErrorderMesg1
    exit $Errcode
fi

# judge jps
if ! [ "`$Which jps 2>/dev/null`" ];then
    echo -e $ErrorderMesg2
    exit $Errcode
fi

# judge process exist
if [ "`jps |grep -E $Process'\.'`" ];then 
    echo -e $ErrproMesg1
    exit $Errcode
fi

# exec 
if [ -f $AbsPath/args.conf ];then
    arg=`cat $AbsPath/args.conf|grep -v ^#`
    if [ -z "$arg" ];then
        echo -e $ErrconfMesg1
        exit $Errcode
    fi
else
    echo -e $ErrconfMesg2
    exit $Errcode
fi

java $arg &
rm -f $AbsPath/*.pid
echo $! >$AbsPath/$!.pid